package com.nursinghome.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.nursinghome.bean.Category;
import com.nursinghome.bean.CategoryGoods;
import com.nursinghome.common.mapper.CategoryGoodsMapper;
import com.nursinghome.common.service.CategoryGoodsService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 程硕
* @createDate 2025-04-07 00:52:14
*/
@Service
public class CategoryGoodsServiceImpl implements CategoryGoodsService{
    @Autowired
    private CategoryGoodsMapper categoryGoodsMapper;
    @Override
    public Boolean insert(CategoryGoods categoryGoods) throws NurisingHomeException {
        // 判断是否同一分类同一商品
        List<Category> categories = categoryGoodsMapper
                .selectAll(new CategoryGoods(null,
                        categoryGoods.getCategoryId(),
                        categoryGoods.getGoodsId(),
                        null,
                        null));
        if (!categories.isEmpty()){
            throw new NurisingHomeException("该分类下已存在该商品");
        }
        return categoryGoodsMapper.insert(categoryGoods) > 0;
    }

    @Override
    public Boolean update(CategoryGoods categoryGoods) throws NurisingHomeException {
        //判断是否存在该商品
        CategoryGoods selectById = categoryGoodsMapper.selectById(categoryGoods.getId());
        if (ObjectUtil.isNotEmpty(selectById)){
            // 判断是否同一分类同一商品
            List<Category> categories = categoryGoodsMapper
                    .selectAll(new CategoryGoods(null,
                            categoryGoods.getCategoryId(),
                            categoryGoods.getGoodsId(),
                            null,
                            null));
            if (!categories.isEmpty()){
                throw new NurisingHomeException("该分类下已存在该商品");
            }
            return categoryGoodsMapper.update(categoryGoods) > 0;
        }else {
            throw new NurisingHomeException("分类不存在");
        }
    }

    @Override
    public Boolean delete(Integer id) throws NurisingHomeException {
        return null;
    }

    @Override
    public Boolean deleteByGoodsId(Integer goodsId) throws NurisingHomeException {
        return categoryGoodsMapper.deleteByGoodsId(goodsId) > 0;
    }
    @Override
    public Boolean deleteByCategoryId(Integer categoryId) throws NurisingHomeException {
        return categoryGoodsMapper.deleteByGoodsId(categoryId) > 0;
    }
    @Override
    public CategoryGoods selectById(Integer id) {
        return null;
    }

    @Override
    public List<CategoryGoods> selectAll() {
        return null;
    }
}
